import Vue from 'vue'
import Router from 'vue-router'
// import HelloWorld from '@/components/HelloWorld'
import Login from '@/components/Login'
import Home from '@/components/Home'
import Welcome from '@/components/Welcome'
import Users from '@/components/Users'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/login',
      component: Login
    },
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      component: Home,
      children: [
        {path: '/welcome', component: Welcome},
        {path: '/users', component: Users}
      ],
      redirect: '/welcome'
    }
  ]
})

// 给router设置路由守卫，非登陆用户禁止访问后台页面
router.beforeEach((to, from, next) => {
  // 获取token，并判断是否存在
  var token = window.sessionStorage.getItem('token')
  if (token === null && to.path !== '/login') {
    // 强行跳转会登陆页面
    return next('/login')
  }
  // 路由继续执行
  next()
})

export default router
